IF EXISTS (SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID('TagInsert') AND sysstat & 0xf = 4)
    DROP PROCEDURE [TagInsert]
GO
CREATE PROCEDURE [TagInsert]
(
    @TagId int = null output,
    @Value nvarchar,
    @CountOfPaint int,
    @CreateDate datetime,
    @UpdateDate datetime
)
AS
BEGIN
	SET NOCOUNT ON
	INSERT INTO [tag](
        [value],
        [count_of_paint],
        [create_date],
        [update_date]
	)
	VALUES(
        @Value,
        @CountOfPaint,
        @CreateDate,
        @UpdateDate
	)
	SELECT @TagId = SCOPE_IDENTITY()
END
GO

IF EXISTS (SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID('TagUpdate') AND sysstat & 0xf = 4)
    DROP PROCEDURE [TagUpdate]
GO
CREATE PROCEDURE [TagUpdate]
(
	@TagId int,
    @Value nvarchar,
    @CountOfPaint int,
    @CreateDate datetime,
    @UpdateDate datetime
)
AS
BEGIN
	
	UPDATE [tag]
	SET
        [value]=@Value,
        [count_of_paint]=@CountOfPaint,
        [create_date]=@CreateDate,
        [update_date]=@UpdateDate
	WHERE
		[tag_id] = @TagId

END
GO

IF EXISTS (SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID('TagDelete') AND sysstat & 0xf = 4)
    DROP PROCEDURE [TagDelete]
GO
CREATE PROCEDURE [TagDelete]
(
	@TagId int	
)
AS
BEGIN
	
	DELETE
	FROM [tag]
    WHERE
		[tag_id] = @TagId
END
GO

IF EXISTS (SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID('TagSelectOne') AND sysstat & 0xf = 4)
    DROP PROCEDURE [TagSelectOne]
GO
CREATE PROCEDURE [TagSelectOne]
(
	@TagId int
)
AS
BEGIN
	SET NOCOUNT ON	
	SELECT
		*
	FROM [tag] (NOLOCK)
	WHERE
		([tag_id] = @TagId)

END
GO

IF EXISTS (SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID('TagSelectAll') AND sysstat & 0xf = 4)
    DROP PROCEDURE [TagSelectAll]
GO

CREATE PROCEDURE [TagSelectAll]
AS
BEGIN

	SET NOCOUNT ON
	SELECT
		tag_id AS TagId,
		Value
	FROM [tag] (NOLOCK)

END
GO
